/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.yangxu.hospital.system.modules.system.service.impl;

import com.yangxu.hospital.common.utils.FileUtil;
import com.yangxu.hospital.common.utils.PageResult;
import com.yangxu.hospital.common.utils.PageUtil;
import com.yangxu.hospital.system.modules.system.domain.SysOrder;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yangxu.hospital.system.modules.system.service.SysOrderService;
import com.yangxu.hospital.system.modules.system.domain.vo.SysOrderQueryCriteria;
import com.yangxu.hospital.system.modules.system.mapper.SysOrderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @description 服务实现
* @author 杨旭
* @date 2024-12-20
**/
@Service
@RequiredArgsConstructor
public class SysOrderServiceImpl extends ServiceImpl<SysOrderMapper, SysOrder> implements SysOrderService {

    private final SysOrderMapper sysOrderMapper;

    @Override
    public PageResult<SysOrder> queryAll(SysOrderQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(sysOrderMapper.findAll(criteria, page));
    }

    @Override
    public List<SysOrder> queryAll(SysOrderQueryCriteria criteria){
        return sysOrderMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(SysOrder resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysOrder resources) {
        SysOrder sysOrder = getById(resources.getId());
        sysOrder.copy(resources);
        saveOrUpdate(sysOrder);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<SysOrder> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SysOrder sysOrder : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户ID，关联到sys_user表", sysOrder.getUserId());
            map.put("预约ID，关联到预约表", sysOrder.getAppointmentId());
            map.put("订单号，唯一标识每个订单", sysOrder.getOrderNumber());
            map.put("订单总金额，精确到小数点后两位", sysOrder.getTotalAmount());
            map.put("支付方式，如支付宝、微信支付、银行卡等", sysOrder.getPaymentMethod());
            map.put("支付状态：0 - 未支付；1 - 已支付；2 - 支付失败", sysOrder.getPaymentStatus());
            map.put("支付时间，记录实际支付完成的时间", sysOrder.getPaymentTime());
            map.put("第三方交易号，例如支付宝交易号", sysOrder.getThirdPartyTransactionId());
            map.put("退款状态：0 - 未退款；1 - 申请中；2 - 已退款；3 - 退款失败", sysOrder.getRefundStatus());
            map.put("退款金额，如果发生部分退款，这里记录实际退款的金额", sysOrder.getRefundAmount());
            map.put("退款原因，简要描述退款的原因，如“重复支付”、“医生取消预约”等", sysOrder.getRefundReason());
            map.put("退款时间，记录实际退款完成的时间", sysOrder.getRefundTime());
            map.put("创建时间，订单创建的时间", sysOrder.getCreateTime());
            map.put("更新时间，每次修改订单时自动更新此字段", sysOrder.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}